package com.alan.filter;

import com.alan.entity.student;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/student")
public class LoginStudentFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = ((HttpServletRequest) request).getSession();
        student student = (student) session.getAttribute("student");
        if (student == null) {
            ((HttpServletResponse) response).sendRedirect("index.jsp");
        } else {
            chain.doFilter(request, response);
        }
    }
}
